package com.xhh.adjust.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhh.adjust.mapper.FaultyMapper;
import com.xhh.adjust.service.FaultyService;
import com.xhh.model.adjust.pojos.Faulty;
import com.xhh.model.common.dtos.ResponseResult;
import com.xhh.model.common.enums.AppHttpCodeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class FaultyServiceImpl extends ServiceImpl<FaultyMapper, Faulty> implements FaultyService {
    /**
     * 根据院系编码获取院系信息
     * @param faultyCode
     * @return
     */
    @Override
    public ResponseResult getFaultyByCode(String faultyCode) {
        // 1. 校验参数
        if (StrUtil.isEmpty(faultyCode)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 发起查询
        Faulty faulty = lambdaQuery()
                .eq(Faulty::getFaultyCode, faultyCode)
                .one();
        if (faulty == null || faulty.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 3. 返回结果
        return ResponseResult.okResult(faulty);
    }

    /**
     * 根据单位id获取院系列表
     * @param unityId
     * @return
     */
    @Override
    public ResponseResult getFaultyByUnityId(Long unityId) {
        // 1. 校验参数
        if (unityId == null || unityId == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 查询
        List<Faulty> list = lambdaQuery()
                .eq(Faulty::getUnityId, unityId)
                .list();
        if (CollUtil.isEmpty(list)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 3. 返回结果
        return ResponseResult.okResult(list);
    }
}
